package com.wang.common.util.config;

import lombok.extern.log4j.Log4j2;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

/**
 * 工具类的配置项
 * @author Zjx
 */
@Log4j2
@Configuration
public class UtilConfig {
    private static int CPU_COUNT = Runtime.getRuntime().availableProcessors() * 3;

    /**
     * 使用代码创建Spring的Bean对象
     */
    @Bean
    public ThreadPoolTaskExecutor executor(){
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        //配置核心线程数
        taskExecutor.setCorePoolSize(CPU_COUNT);
        //配置最大线程数
        taskExecutor.setMaxPoolSize(CPU_COUNT * 2 + 1);
        //配置队列大小
        taskExecutor.setQueueCapacity(1000);
        /* 设置线程前缀 */
        taskExecutor.setThreadNamePrefix("自定义线程-");
        /* 初始化 */
        taskExecutor.initialize();
        log.info("==多线程初始化:核心线程数:{},最大线程数:{},队列大小:{}==", taskExecutor.getCorePoolSize(), taskExecutor.getMaxPoolSize(),
                taskExecutor.getQueueCapacity());
        return taskExecutor;
    }
}
